package com.fqyd.campustransaction.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fqyd.campustransaction.mapper.CartMapper;
import com.fqyd.campustransaction.model.Cart;
import com.fqyd.campustransaction.model.CartExample;
import com.fqyd.campustransaction.service.CartService;

/**
 * 购物车
 */
@Service
public class CartServiceImpl implements CartService {

	@Resource
	private CartMapper mapper;

	@Override
	public List<Cart> getAll() {
		return mapper.selectByExample(new CartExample());
	}

	@Override
	public void add(Cart cart) {
		mapper.insert(cart);
	}

	@Override
	public void update(Cart cart) {
		mapper.updateByPrimaryKey(cart);
	}

	@Override
	public void delete(String id) {
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	public Cart getById(String id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Cart> getByUserId(String userId) {
		CartExample example = new CartExample();
		CartExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		return mapper.selectByExample(example);
	}

	@Override
	public boolean checkGoodsId(String userId, String goodsId) {
		CartExample example = new CartExample();
		CartExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andGoodsIdEqualTo(goodsId);
		List<Cart> list = mapper.selectByExample(example);
		return list != null && list.size() > 0 ? false : true;
	}

	@Override
	public void deleteCart(String userId, String goodsId) {
		CartExample example = new CartExample();
		CartExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andGoodsIdEqualTo(goodsId);
		mapper.deleteByExample(example);
	}
}
